<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Default_Model_Rate extends ZF_DB{
    protected $_table_name = "rate" ;
    
    protected $_id_item;
    protected $_id_user;
    protected $_type1;
    protected $_type2;
    protected $_type3;
    protected $_overall;
    protected $_comment;
    protected $_pre_hospital = "hospital";
    protected $_pre_faculty = "faculty";
    protected $_pre_test = "test";
    
    public function getPrefixHospital(){
        return $this->_pre_hospital;
    }
    public function getPrefixFaculty(){
        return $this->_pre_faculty;
    }
    public function getPrefixTest(){
        return $this->_pre_test;
    }
    public function getIdItem(){
        return $this->_id_item;
    }
    public function setIdItem($id_item){
        $this->_id_item = $id_item;
    }
    public function getIdUser(){
        return $this->_id_user;
    }
    public function setIdUser($id_user){
        $this->_id_user = $id_user;
    }
    public function getType1(){
        return $this->_type1;
    }
    public function setType1($type1){
        $this->_type1 = $type1;
    }
    public function getType2(){
        return $this->_type2;
    }
    public function setType2($type2){
        $this->_type2 = $type2;
    }
    public function getType3(){
        return $this->_type3;
    }
    public function setType3($type3){
        $this->_type3 = $type3;
    }
    public function getOverall(){
        return $this->_overall;
        
    }
    public function setOverall($overall){
        $this->_overall = $overall;
    }
    
    public function getComment(){
        return $this->_comment;
    }
    public function setComment($comment){
        $this->_comment = $comment;
    }
    
    public function insert($id_item, $id_user, $type1, $type2, $type3, $comment){
        $overall = (floatval($type1) + floatval($type2) + floatval($type3)) / 3;
        if($id_item && $id_user && $type1 && $type2
                && $type3 && $overall && $comment){             
                $data =  array(
                    'id_item' =>$id_item,
                    'id_user'=>$id_user,
                    'type1' =>$type1,
                    'type2' =>$type2,
                    'type3'=>$type3,
                    'overall'=>$overall,
                    'comment'=>$comment);
            return  $this->db->insert($this->_table_name,$data);
	}
        return -1;
	
    }
   
    public function update(){
        $where  = array(
            'id_item=?' => $this->_id_item,
            'id_user=?' => $this->_id_user
        );
        $overall = (floatval($this->_type1) + floatval($this->_type2) + floatval($this->_type3)) / 3;
	$data =  array(
                    'type1' =>$this->_type1,
                    'type2'=>$this->_type2,
                    'type3'=>$this->_type3,
                    'overall'=>(string)$overall,
                    'comment'=>$this->_comment);
        return $this->db->update(
                $this->_table_name,
                $data,$where);
    }
    public function getByItemIdUserId($id_item, $id_user){
        if($id_item && $id_user)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.id_item = ?", $id_item)
                        ->where("p.id_user = ?", $id_user);
		
		$data = $this->db->fetchRow($select);
		return $data;
	}
	return -1;
    }
    
    public function getByItemId($id_item){
        if($id_item)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.id_item = ?", $id_item)
                        ->order("p.overall DESC");
		$data = $this->db->fetchAll($select);
		return $data;
	}
	return -1;
    }
    
    public function getByItemIdTop1($id_item){
        if($id_item)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->limit(1,0)
                        ->where("p.id_item = ?", $id_item)
                        ->order("p.overall DESC");
		$data = $this->db->fetchAll($select);
		return $data;
	}
	return -1;
    }
    
    public function getByUserId($id_user){
        if($id_user)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.id_user = ?", $id_user)
                        ->order("p.overall DESC");
		$data = $this->db->fetchAll($select);
		return $data;
	}
	return -1;
    }
}
?>
